package com.itg.vms.dao.impl;

import com.itg.vms.dao.CollectMoviesDao;
import com.itg.vms.entity.CollectMovies;
import com.itg.vms.entity.Movies;
import com.itg.vms.util.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectMoviesDaoImpl implements CollectMoviesDao {
    QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

    /**
     * 收藏影片实现类
     * @param userId
     * @param movie_id
     * @return
     */
    @Override
    public boolean addCollectMovies(Long userId, Long movie_id,String username) {
        String sql = "insert into collect(user_id,movie_id,is_pay,create_by,create_time) values(?,?,?,?,sysdate())";
        int i=0;
        try {
             i = queryRunner.update(sql, userId, movie_id, 0,username);
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return i>0;
    }

    /**
     * 查询收藏的影片
     * @param userId
     * @return
     */
    @Override
    public List<Movies> selectAllCollectMovies(Long userId) {
        List<Movies> list = new ArrayList<>();
        String sql = "select m.movie_Id,m.movie_Picture, m.movie_Name,c.cid from collect c,movie m WHERE c.user_id= ? AND c.movie_id=m.movie_Id;";
        try {
            list = queryRunner.query(sql, new BeanListHandler<Movies>(Movies.class), userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 实现删除影片
     * @param userId
     * @param movie_id
     * @return
     */
    @Override
    public boolean deleteCollectMovies(Long userId, Long movie_id) {
        String sql = "delete from collect where user_id = ? and movie_id =?";
        int i = 0;
        try {
             i = queryRunner.update(sql, userId, movie_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i>0;
    }


}
